Warning in st_point_on_surface.sfc(coords): st_point_on_surface may not give
correct results for longitude/latitude data
Tiếp giáp với đơn vị diện tích số 5, id =5
id <-5# area idbando$neighbors <-"other"bando$neighbors[id] <-"area"bando$neighbors[nb[[id]]] <-"neighbors"ggplot(bando) +geom_sf(aes(fill = neighbors)) +theme_bw() +scale_fill_manual(values =c("gray30", "gray", "white"))
Liên kết dữ liệu điều tra vào shape file ?
Tải dữ file điểm nhà
Xem bản đồ
Láng giềng dựa trên k láng giềng gần nhất
Thư viện thống kê
# Neighbors based on 3 nearest neighbors, càng lớn càng có nhiều láng giềngcoo <-st_centroid(bando)
Warning: st_centroid assumes attributes are constant over geometries
nb <-knn2nb(knearneigh(coo, k =2)) # k number nearest neighborsplot(st_geometry(bando), border ="lightgray")plot.nb(nb, st_geometry(bando), add =TRUE)
Warning in st_point_on_surface.sfc(coords): st_point_on_surface may not give
correct results for longitude/latitude data
Láng giềng dựa trên khoảng cách
# Neighbors based on distance: trong đường kính d1 đến d2 Từ huyện này sang kia tối thiểu 7km, xa nhất 12nb <-dnearneigh(x =st_centroid(bando), d1 =6, d2 =12)
Warning: st_centroid assumes attributes are constant over geometries
Warning in st_point_on_surface.sfc(coords): st_point_on_surface may not give
correct results for longitude/latitude data
Tìm sao cho mỗi khu vực có ít nhất 1 hàng xóm qua 2 bước
# Lấy trọng tâm, chuyển đổi danh sách thành lớp với các id láng giềngcoo <-st_centroid(bando)
Warning: st_centroid assumes attributes are constant over geometries
# k is the number nearest neighborsnb1 <-knn2nb(knearneigh(coo, k =1))
Warning in knn2nb(knearneigh(coo, k = 1)): neighbour object has 2 sub-graphs
# Sau đó truyền danh sách nb và tính toán tóm tắtdist1 <-nbdists(nb1, coo)summary(unlist(dist1))
Min. 1st Qu. Median Mean 3rd Qu. Max.
6.130 7.165 7.610 7.732 8.250 9.733
# Kết quả d1=6.130 và d2 = 9.733
Láng giềng của bậc k dựa trên tiếp giáp
library(spdep)nb <-poly2nb(bando, queen =TRUE)nblags <- spdep::nblag(neighbours = nb, maxlag =2) # tạo danh sách chứa láng giềng bậc 1 và bậc 2# Neighbors of first orderplot(st_geometry(bando), border ="lightgray")plot.nb(nblags[[1]], st_geometry(bando), add =TRUE)
Warning in st_point_on_surface.sfc(coords): st_point_on_surface may not give
correct results for longitude/latitude data
Láng giềng bậc 2
# Neighbors of second orderplot(st_geometry(bando), border ="lightgray")plot.nb(nblags[[2]], st_geometry(bando), add =TRUE)
Warning in st_point_on_surface.sfc(coords): st_point_on_surface may not give
correct results for longitude/latitude data
Hàm spdep::nblag_cumul() tích lũy một danh sách láng giềng bị trễ và nblag() trả về danh sách láng giềng duy nhất.
# Neighbors of order 1 until order 2nb <- spdep::poly2nb(bando, queen =TRUE)nblagsc <- spdep::nblag_cumul(nblags)plot(st_geometry(bando), border ="lightgray")plot.nb(nblagsc, st_geometry(bando), add =TRUE)
Warning in st_point_on_surface.sfc(coords): st_point_on_surface may not give
correct results for longitude/latitude data
Ma trận láng giềng
Ma trận trọng số không gian dựa trên danh sách láng giềng dạng nhị phân
Có nhiều dạng style = B là mã hóa nhị phân cơ bản và W là được chuẩn hóa theo hàng (1/số láng giềng)
nb <-poly2nb(bando, queen =TRUE)nbw <- spdep::nb2listw(nb, style ="W")nbw$weights[1:3]
Reading layer `bacninh3' from data source `F:\five\R\thuchanhR\shape\bacninh3.shp' using driver `ESRI Shapefile'
Simple feature collection with 125 features and 13 fields
Geometry type: MULTIPOLYGON
Dimension: XY
Bounding box: xmin: 105.9034 ymin: 20.96926 xmax: 106.3112 ymax: 21.26444
Geodetic CRS: WGS 84
map$vble <-rnorm(125,20,5)#map$vble <- map$MEDV # Giá trị trung bình bán nhà trong khu vựctm_shape(map)+tm_polygons("vble")
Chỉ số toàn cục Moran’s I
Hàm kiểm định moran.test()
H_0: I \leq E[I] (tự tương quan âm hoặc không tự tương quan)
H_1: I > E[I] (tự tương quan dương)
# Neighborslibrary(spdep)nb <-poly2nb(map, queen =TRUE) # queen shares point or bordernbw <-nb2listw(nb, style ="W")# Global Moran's Igmoran <-moran.test(map$vble, nbw,alternative ="greater")gmoran
Moran I test under randomisation
data: map$vble
weights: nbw
Moran I statistic standard deviate = -0.39394, p-value = 0.6532
alternative hypothesis: greater
sample estimates:
Moran I statistic Expectation Variance
-0.029881112 -0.008064516 0.003067050
Mô phỏng
Đồ thị scatter về Moran I
moran.plot(map$vble, nbw)
Chỉ số Moran I cục bộ
lmoran <-localmoran(map$vble, nbw, alternative ="greater")head(lmoran)